Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error position reported by the lexer #186

Merged
merged 1 commit into from
Oct 29, 2024
Merged

Conversation

makenowjust
Copy link
Collaborator

@makenowjust makenowjust commented Oct 28, 2024

This introduces two helper methods, errorfAtPosition and panicfAtPosition, and uses them to report errors caused by tokenizing string-like literals and skipping comments.

This fix improves error positions of unclosed string/comment and invalid escape sequences.

Before:

$ go run ./tools/parse/main.go 'SELECT "foo'
syntax error: :1:8: unclosed string literal

  1:  SELECT "foo
             ^
exit status 1
$ go run ./tools/parse/main.go 'SELECT "foo\UFFFFFFFF'
syntax error: :1:8: invalid escape sequence: invalid code point: U+FFFFFFFF

  1:  SELECT "foo\UFFFFFFFF
             ^
exit status 1
$ go run ./tools/parse/main.go '/* comment'
syntax error: :1:11: unclosed comment

  1:  /* comment
                ^
exit status 1

After:

$ go run ./tools/parse/main.go 'SELECT "foo'
syntax error: :1:8: unclosed string literal

  1:  SELECT "foo
             ^~~~
exit status 1
$ go run ./tools/parse/main.go 'SELECT "foo\UFFFFFFFF'
syntax error: :1:12: invalid escape sequence: invalid code point: U+FFFFFFFF

  1:  SELECT "foo\UFFFFFFFF
                 ^~~~~~~~~~
exit status 1
$ go run ./tools/parse/main.go '/* comment'
syntax error: :1:1: unclosed comment

  1:  /* comment
      ^~~~~~~~~~
exit status 1

@apstndb
Copy link
Contributor

apstndb commented Oct 28, 2024

LGTM!

Out of curiosity, I checked to see if there was an error with the multi-line comment.

$ go run ./tools/parse/main.go "SELECT
/*
one
two
three"
2024/10/28 21:52:13 syntax error: :2:1: unclosed comment

  2:  /*
  3:  one
  4:  two
  5:  three
exit status 1

There doesn't seem to be anything strange.

This introduces two helper method `errorfAtPosition` and
`panicfAtPosition`, and uses them for reporting errors caused by
tokenizing string-like literals and skipping comments.
@makenowjust makenowjust merged commit e50eded into main Oct 29, 2024
4 checks passed
@makenowjust makenowjust deleted the improve-lex-error branch October 29, 2024 13:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants